package com.shkco.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.shkco.domain.User;
import com.shkco.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "order-getUserById",
            blockHandler = "orderBlockHandler",
            fallback = "orderFallback")
    public User getUserById(@PathVariable("id") Long id){
//        int a = 1/0;
        return userService.getUserById(id);
    }

    public User orderBlockHandler(@PathVariable("id") Long id, BlockException e){
        return new User(-1L,"限流了！","请稍后再试！");
    }

    public User orderFallback(@PathVariable("id") Long id, Throwable e){
        return new User(-1L,"熔断了！","请稍后再试！");
    }
}
